﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class Student_TestInProgress : System.Web.UI.Page
{
    int pos, duration, questionid, ismultipleoption,totalTime;
    DateTime EntryTime, ExitTime;
    DataSet dsQustn;
    ListItem lst = new ListItem();
    Information ansObj = new Information();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Pos"] = 0;
            pos = (int)Session["Pos"];
            GetTestinProgressFields();
            fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
            ansObj.answer.Clear();
            for (int i = 0; i < dsQustn.Tables[0].Rows.Count; i++)
            {
                ansObj.answer.Add(Convert.ToInt32(dsQustn.Tables[0].Rows[i][2]), "NA");
            }
            Session["Answers"] = ansObj;
            displayQuestion(0);
            displayOption(questionid);
        }
        dsQustn = (DataSet)Session["DsQuestion"];
        questionid = (int)Session["QuestionId"];
        ismultipleoption = (int)Session["IsMultiple"];
        pos = (int)Session["Pos"];
        EntryTime = (DateTime)Session["EntryTime"];
        ExitTime = (DateTime)Session["ExitTime"];
        ansObj = (Information)Session["Answers"];

    }

    protected void GetTestinProgressFields()
    {
        DataTable dtFields;
        dtFields = DALKAT.GetTestInProgressFields(Int32.Parse(Request.QueryString["TestId"].ToString()));
        if (dtFields.Rows.Count != 0)
        {
            lbltstname.Text = dtFields.Rows[0][1].ToString();
            lbltotalqustn.Text = dtFields.Rows[0][2].ToString();
            duration = Convert.ToInt32(dtFields.Rows[0][3]);
            Session["EntryTime"] = DateTime.Now;
            EntryTime = (DateTime)Session["EntryTime"];
            Session["ExitTime"] = DateTime.Now.AddMinutes(Convert.ToDouble(duration));
            ExitTime = (DateTime)Session["ExitTime"];
            lbltime.Text = Convert.ToInt32(ExitTime.Subtract(DateTime.Now).TotalMinutes).ToString();
         }
    }

    private void fillQuestionDs(int testId)
    {
        Session["DsQuestion"] = DALKAT.GetQuestions(testId);
        dsQustn = (DataSet)Session["DsQuestion"];
        dsQustn.Tables[0].Columns.Add("QuestionNo", typeof(System.Int32));
        for (int i = 0; i < dsQustn.Tables[0].Rows.Count; i++)
            dsQustn.Tables[0].Rows[i]["QuestionNo"] = i + 1;
    }

    private void displayQuestion(int pos)
    {
        lblquest.Text = dsQustn.Tables[0].Rows[pos][1].ToString();
        Session["QuestionId"] = dsQustn.Tables[0].Rows[pos][2];
        questionid = (int)Session["QuestionId"];
        Session["IsMultiple"] = dsQustn.Tables[0].Rows[pos][3];
        ismultipleoption = (int)Session["IsMultiple"];
        lblqno.Text = (Convert.ToInt32(dsQustn.Tables[0].Rows[pos]["QuestionNo"])).ToString();
    }

    private void displayOption(int questionid)
    {
        DataSet dsoptn = DALKAT.GetOptions(questionid);
        Rdblist.Items.Clear();
        Chklist.Items.Clear();

        if (ismultipleoption == 0)
        {
            for (int i = 0; i < dsoptn.Tables[0].Rows.Count; i++)
            {
                lst = new ListItem();
                lst.Text = dsoptn.Tables[0].Rows[i][1].ToString();
                lst.Value = dsoptn.Tables[0].Rows[i][0].ToString();
                Rdblist.Items.Add(lst);
            }
        }
        else
        {
            for (int i = 0; i < dsoptn.Tables[0].Rows.Count; i++)
            {
                lst = new ListItem();
                lst.Text = dsoptn.Tables[0].Rows[i][1].ToString();
                lst.Value = dsoptn.Tables[0].Rows[i][0].ToString();
                Chklist.Items.Add(lst);
            }
        }
    }

    private void calTotalTimeTaken()
    {
        totalTime = Convert.ToInt32((DateTime.Now - EntryTime).Minutes);
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        Session["Answers"] = ansObj;
        if (DateTime.Now < ExitTime)
        {
            lbltime.Text = Convert.ToInt32(ExitTime.Subtract(DateTime.Now).TotalMinutes).ToString();
            pos = (int)Session["Pos"];
            pos++;
            Session["Pos"] = pos;
            if (pos == dsQustn.Tables[0].Rows.Count - 1)
            {
                displayQuestion(pos);
                displayOption(questionid);
                getSelectedOption();
                btnNext.Text = "ScoreCard";
            }
            else if (pos > dsQustn.Tables[0].Rows.Count - 1)
            {
                pos = dsQustn.Tables[0].Rows.Count - 1;
                calTotalTimeTaken();
                //Response.Redirect("~/Student/ScoreCard.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);
                Response.Redirect("~/Student/Score.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);

            }

            else
            {
                displayQuestion(pos);
                displayOption(questionid);
                getSelectedOption();
            }
        }
        else
        {
            calTotalTimeTaken();
            //Response.Redirect("~/Student/ScoreCard.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);
            Response.Redirect("~/Student/Score.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);

        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["Answers"] = ansObj;
        if (DateTime.Now < ExitTime)
        {
            lbltime.Text = Convert.ToInt32(ExitTime.Subtract(DateTime.Now).TotalMinutes).ToString();
            btnNext.Text = "Next";
            pos = (int)Session["Pos"];
            pos--;
            Session["Pos"] = pos;
            if (pos < 0)
                pos = 0;
            else
            {
                displayQuestion(pos);
                displayOption(questionid);
                getSelectedOption();
            }
        }
        else
        {
            calTotalTimeTaken();
            //Response.Redirect("~/Student/ScoreCard.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);
            Response.Redirect("~/Student/Score.aspx?TestId=" + Int32.Parse(Request.QueryString["TestId"].ToString()) + "&timeTaken=" + totalTime);

         }

    }

    private void getSelectedOption()
    {
        ansObj=(Information)Session["Answers"];
        if (ismultipleoption == 0)
        {
            foreach (var s in ansObj.answer)
            {
                if (s.Key == questionid)
                    Rdblist.SelectedIndex = Rdblist.Items.IndexOf(Rdblist.Items.FindByValue(s.Value));
            }
        }
        else
        {
            string options = "";
            string[] option;
            foreach (var s in ansObj.answer)
            {
                if (s.Key == questionid)
                    options = s.Value;
            }
            option = options.Split('|');

            for (int i = 0; i < Chklist.Items.Count; i++)
            {
                for (int j = 0; j < option.Length - 1; j++)
                {
                    if (Chklist.Items[i].Value == option[j])
                        Chklist.Items[i].Selected = true;
                }
            }
        }

    }

    protected void Chklist_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ans = "";
        if (ansObj.answer.ContainsKey(questionid))
            ansObj.answer.Remove(questionid);
        for (int i = 0; i < Chklist.Items.Count; i++)
        {
            if (Chklist.Items[i].Selected == true)
                ans += Chklist.Items[i].Value + "|";
        }
        ansObj.answer.Add(questionid, ans);
    }

    protected void Rdblist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ansObj.answer.ContainsKey(questionid))
            ansObj.answer.Remove(questionid);
        ansObj.answer.Add(questionid, Rdblist.SelectedValue);
    }
}